package com.example.shop_front_api.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.shop_front_api.entity.Cart;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface CartMapper extends BaseMapper<Cart> {

    @Update("update shop_carts set quantity = quantity + #{quantity} where member_id = #{memberId} and goods_id = #{productId}")
    int updateCart(@Param("memberId") Integer memberId, @Param("productId") Integer productId, @Param("quantity") Integer quantity);

    @Select("select SUM(pay_price * quantity) as total_amount from shop_carts where member_id = #{memberId} and selected = 1")
    Double getCartTotalAmount(@Param("memberId") Integer memberId);

    @Update("update shop_carts set selected = #{selected} where id = #{id}")
    int updateSelectedById(@Param("id") Long id, @Param("selected") Integer selected);

    @Update("update shop_carts set selected = #{selected} where id in ${ids}")
    int batchUpdateSelected(@Param("ids") String ids, @Param("selected") Integer selected);

    @Update("update shop_carts set quantity = #{quantity} where id = #{id}")
    int updateCartQuantity(@Param("id") Integer id, @Param("quantity") Integer quantity);
}